package com.idanchuang.component.base.exception.core.asserts;

import com.idanchuang.component.base.exception.core.ExFactory;
import com.idanchuang.component.base.exception.exception.AbstractException;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;

/**
 *
 * 业务断言, 当不符合预期时, 抛出BusinessException异常
 * 各个断言逻辑详细注释参见: {@link org.springframework.util.Assert}
 *
 * @author yjy
 * Created at 2021/3/31 4:10 下午
 */
public abstract class ExBusinessAssert {

    private static AbstractException getException(String messagePattern, Object... messages) {
        return ExFactory.throwBusiness(messagePattern, messages);
    }

    public static void isTrue(boolean expression, String messagePattern, Object... messages) {
        if (!expression) {
            throw getException(messagePattern, messages);
        }
    }

    public static void isTrue(boolean expression, Supplier<String> messageSupplier) {
        if (!expression) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    public static void isNull(@Nullable Object object, String messagePattern, Object... messages) {
        if (object != null) {
            throw getException(messagePattern, messages);
        }
    }

    public static void isNull(@Nullable Object object, Supplier<String> messageSupplier) {
        if (object != null) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    public static void notNull(@Nullable Object object, String messagePattern, Object... messages) {
        if (object == null) {
            throw getException(messagePattern, messages);
        }
    }

    public static void notNull(@Nullable Object object, Supplier<String> messageSupplier) {
        if (object == null) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    public static void hasLength(@Nullable String text, String messagePattern, Object... messages) {
        if (!StringUtils.hasLength(text)) {
            throw getException(messagePattern, messages);
        }
    }

    public static void hasLength(@Nullable String text, Supplier<String> messageSupplier) {
        if (!StringUtils.hasLength(text)) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    public static void hasText(@Nullable String text, String messagePattern, Object... messages) {
        if (!StringUtils.hasText(text)) {
            throw getException(messagePattern, messages);
        }
    }

    public static void hasText(@Nullable String text, Supplier<String> messageSupplier) {
        if (!StringUtils.hasText(text)) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    public static void doesNotContain(@Nullable String textToSearch, String substring, String messagePattern, Object... messages) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
                textToSearch.contains(substring)) {
            throw getException(messagePattern, messages);
        }
    }

    public static void doesNotContain(@Nullable String textToSearch, String substring, Supplier<String> messageSupplier) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
                textToSearch.contains(substring)) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    public static void notEmpty(@Nullable Object[] array, String messagePattern, Object... messages) {
        if (ObjectUtils.isEmpty(array)) {
            throw getException(messagePattern, messages);
        }
    }

    public static void notEmpty(@Nullable Object[] array, Supplier<String> messageSupplier) {
        if (ObjectUtils.isEmpty(array)) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    public static void noNullElements(@Nullable Object[] array, String messagePattern, Object... messages) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw getException(messagePattern, messages);
                }
            }
        }
    }

    public static void noNullElements(@Nullable Object[] array, Supplier<String> messageSupplier) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw getException(nullSafeGet(messageSupplier));
                }
            }
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, String messagePattern, Object... messages) {
        if (CollectionUtils.isEmpty(collection)) {
            throw getException(messagePattern, messages);
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, Supplier<String> messageSupplier) {
        if (CollectionUtils.isEmpty(collection)) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    public static void notEmpty(@Nullable Map<?, ?> map, String messagePattern, Object... messages) {
        if (CollectionUtils.isEmpty(map)) {
            throw getException(messagePattern, messages);
        }
    }

    public static void notEmpty(@Nullable Map<?, ?> map, Supplier<String> messageSupplier) {
        if (CollectionUtils.isEmpty(map)) {
            throw getException(nullSafeGet(messageSupplier));
        }
    }

    @Nullable
    private static String nullSafeGet(@Nullable Supplier<String> messageSupplier) {
        return (messageSupplier != null ? messageSupplier.get() : null);
    }


}
